package com.it.myd.module.game.saolei.controller;

import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.help.StringHelper;
import com.it.myd.module.game.saolei.bean.SLLei;
import com.it.myd.module.game.saolei.service.SaoLeiService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/module/saolei")
public class SaoLeiController {

    @Autowired
    private SaoLeiService saoLeiService;

    public void createSaoleiTable(HttpServletRequest request){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        saoLeiService.setUser(user);
        SLLei[][] leilist_t = (SLLei[][]) application.getAttribute("leilist");
        if(leilist_t!=null){

        }else{
            saoLeiService.setSaoLeiTable(20,10,40);
            SLLei[][] leilist = saoLeiService.creatSaoleiTable();
            application.setAttribute("leilist", leilist);
        }

    }

    @RequestMapping("getSaoleiTable")
    @ResponseBody
    public Map<String,Object> getSaoleiTable(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        SLLei[][] leilist = (SLLei[][]) application.getAttribute("leilist");
        Map<String, Object> map = new HashMap<>();
        map.put("leilist",leilist);
        Map<String, String> saoleiMap = saoLeiService.getSaoLeiTable();
        map.put("saoleiMap",saoleiMap);
        return map;
    }

    @RequestMapping("clickSaolei")
    @ResponseBody
    public void clickSaoleiTable(HttpServletRequest request){
        int x = Integer.parseInt(request.getParameter("x"));
        int y = Integer.parseInt(request.getParameter("y"));
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        saoLeiService.setUser(user);
        saoLeiService.clickSaoleiTable(x,y);
    }

    @RequestMapping("reset")
    @ResponseBody
    public ReturnValue reset(HttpServletRequest request){
        ServletContext application = request.getServletContext();
        int x = Integer.parseInt(request.getParameter("x"));
        int y = Integer.parseInt(request.getParameter("y"));
        int l = Integer.parseInt(request.getParameter("l"));
        if(x > 0){
            if(x *y < l){
                l = x * y -1;
            }
            saoLeiService.setSaoLeiTable(x,y,l);
        }

        SLLei[][] leilist = saoLeiService.creatSaoleiTable();
        application.setAttribute("leilist", leilist);

        ReturnValue returnValue = new ReturnValue();
        returnValue.isSuccess();
        return returnValue;
    }

}
